package org.xukai.common.spel.utils;

import org.apache.log4j.Logger;

import java.text.DecimalFormat;
import java.util.List;

public class StringUtils {

    private static Logger logger = Logger.getLogger(StringUtils.class);

	public static String join(List list){
		return join(list, ',');
	}
	
    public static String join(List list, char seperateChar){
        if(list == null || list.isEmpty()){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for(Object v : list){
            String vStr = "";
            if(v != null){
                if(v instanceof String){
                    vStr = (String)v;
                }else{
                    vStr = String.valueOf(v);
                }
            }

            if(!isEmpty(vStr) && vStr.contains(",")){
                vStr = vStr.replace(",", "&#44;");
            }
            if(index == 0){
                sb.append(vStr);
            }else{
                sb.append(seperateChar).append(vStr);
            }
            index++;
        }
        return sb.toString();
    }

    public static String nvl(String str){
        if(str == null) return "";
        return str;
    }

    public static Long nvl(Long val){
        if(val == null) return 0L;
        return val;
    }

    public static Integer nvl(Integer val){
        if(val == null) return 0;
        return val;
    }

    public static Double nvl(Double val){
        if(val == null) return 0D;
        return val;
    }

    public static boolean isEmpty(String str){
        if(str == null || str.trim().length() == 0){
            return true;
        }
        return false;
    }

    /**
     * 是否是不正确的整型值
     * @param val == null || val <= 0
     * @return
     */
    public static boolean isErrorNumValue(Integer val){
        if(val == null || val <= 0){
            return true;
        }
        return false;
    }

    /**
     * 是否是不正确的整型值
     * @param val == null || val <= 0
     * @return
     */
    public static boolean isErrorNumValue(Long val){
        if(val == null || val <= 0){
            return true;
        }
        return false;
    }
    /**
     * 是否是正确的整型值
     * @param val != null && val <= 0
     * @return
     */
    public static boolean isCorrectNumValue(Integer val){
        if(val != null && val > 0){
            return true;
        }
        return false;
    }

    /**
     * 是否是正确的整型值
     * @param val != null && val > 0
     * @return
     */
    public static boolean isCorrectNumValue(Long val){
        if(val != null && val > 0){
            return true;
        }
        return false;
    }

    /**
     * 保留两位小数
     * @param v
     * @return
     */
    public static double verifyVDecimalPlaces(Double v){
        if(v == null || v.isNaN()) return 0;
        return verifyVDecimalPlaces(v, 2);
    }

    /**
     * 保留两位小数
     * @param v
     * @return
     */
    public static double verifyVDecimalPlaces(Double v, int median){
        if(v == null || v.isNaN()) return 0;
        double result = 0;
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<median; i++){
            sb.append("0");
        }
        try{
            if(v == null || v == 0) return 0;
            DecimalFormat df=new DecimalFormat("#."+sb.toString());
            result = Double.parseDouble(df.format(v));
        }catch(Exception e){
            logger.error("保留两位小数出错", e);
            return 0;
        }
        return result;
    }
}
